	#include <stdlib.h>
	#include <stdio.h>
	#include "ObjectList.h"
	#include "AbrahamLincoln.h"
	#include "include.h"
	
	ObjectList::ObjectList(bool ownsObjects)
	{
		this->ownsObjects = ownsObjects;
		head = NULL;
	}
	ObjectList::ObjectList()
	{
		ownsObjects = true;
		head = NULL;
	}
	ObjectList::ObjectList(Object * headObject)
	{
		ownsObjects = true;
		head = new List<Object>(headObject, NULL);
	}
	ObjectList::~ObjectList()
	{
		if(!ownsObjects)
			return;
		printf("Deleting object list\n");
		int i = 0;
		List<Object> * search = head;
		while(search)
		{
			printf("object number %d\n",i);
			i++;
			if(search->payload && search->payload->objectId == PLAYER_OBJECT_CLASS)
				search = search->RemoveList();
			else
				search = search->Remove();
		}
		printf("Done deleting object list\n");
		head = NULL;
	}
	void ObjectList::Add(Object * payload)
	{
		head = new List<Object>(payload, head);
	}
	void ObjectList::Draw()
	{
		List<Object> * search = head;
		/*while(search)
		{
			if(search->payload)
				search->payload->DrawCollision();
			search = search->next;
		}
		search = head;*/
		while(search)
		{
			if(search->payload)
				search->payload->Draw();
			search = search->next;
		}	
	}
	void ObjectList::AddGravity()
	{
		List<Object> * search = head;
		
		while(search)
		{
			if(search->payload)
				search->payload->AddGravity();
			search = search->next;
		}
	}
	void ObjectList::CreateObjects()
	{
		List<Object> * search = head;
		
		while(search)
		{
			if(search->payload)
			{
				Object * object = search->payload->CreateObject();
				if(object != NULL)
				{
					Add(object);
				}
			}
			search = search->next;
		}
	}
	void ObjectList::DestroyObjects(AbrahamLincoln * player)
	{
		List<Object> * search = head;
		
		while(search)
		{
			List<Object> * next = search->next;
			if(search->payload)
			{
				Object * object = search->payload->DestroyObject();
				if(object != NULL)
				{
					if(search->payload == object)
					{
						if(search == head)
							head = search->next;
						if(player->beardString->grabObject == search->payload)
							player->beardString->Release();
						
						if(search == head)
							head = search->next;
						search->Remove();
					}
					else
					{
						List<Object> * search2 = head;
						
						while(search2)
						{
							List<Object> * next2 = search2->next;
							if(search2->payload)
							{
								if(search2->payload == object)
								{
									if(player->beardString->grabObject == search->payload)
										player->beardString->Release();
									
									if(search2 == head)
										head = search2->next;
									search2->Remove();
									
								}
							}		
							search2 = next2;
						}
					}
				}
			}
			search = next;
		}
	}
	void ObjectList::CheckCollisions(double elapsedTime, ObjectList * anotherList)
	{
		List<Object> * search = head;
		
		while(search)
		{
			List<Object> * search2 = anotherList->head;
			while(search2)
			{
				search->payload->Collide(search2->payload, elapsedTime);
				search2 = search2->next;	
			}
			search = search->next;
		}
	}
	void ObjectList::CheckCollisions(double elapsedTime)
	{
		List<Object> * search = head;
		
		while(search)
		{
			CheckCollisions(search, elapsedTime);
			search = search->next;
		}
	}
	void ObjectList::CheckCollisions(double elapsedTime, Border * border)
	{
		if(!border)
			return;
			
		List<Object> * search = head;
		//printf("Start border collide\n");
		int i = 0;
		while(search)
		{
			if(search->payload)
			{
				//printf("Check collision %d\n", i);
				border->Collide(search->payload);
			}
			i++;
			search = search->next;
		}
		//printf("End border collide\n");
	}
	void ObjectList::CheckCollisions(List<Object> * start, double elapsedTime)
	{
		List<Object> * search = start->next;
		while(search)
		{
			if(search->payload)
				start->payload->Collide(search->payload, elapsedTime);
			search = search->next;
		}
	}
	void ObjectList::Think(float elapsedTime)
	{
		List<Object> * search = head;
		while(search)
		{
			if(search->payload)
				search->payload->Think(elapsedTime);
			search = search->next;
		}
	}
Object * ObjectList::Action(Vector2D playerPos, Dictionary * gameState, char ** speak)
{
	List<Object> * search = head;
	while(search)
	{
		if(search->payload)
		{
			if((*speak = search->payload->Action(playerPos, gameState)))
			{
				printf("speak = %s\n", *speak);
				return search->payload;
			}
		}
			
		search = search->next;
	}	
	return NULL;	
}
Vector2D ObjectList::GetMidpoint()
{
	List<Object> * search = head;
	Vector2D midPoint = Vector2D(0,0);
	int count = 0;
	while(search)
	{
		if(search->payload)
		{
			midPoint = midPoint + search->payload->position;
			count ++;
		}
			
		search = search->next;
	}
	if(count == 0)
		count = 1;
		
	return midPoint/count;
}
float ObjectList::GetWidth()
{
	List<Object> * search = head;
	
	float width = 0.0;
	while(search)
	{
		if(search->payload)
		{
			List<Object> * search2 = search->next;
			while(search2)
			{
				if(search2->payload)
				{
					float dist = search->payload->position.dist((search2->payload->position));
					if(dist > width)
						width = dist;
				}
				search2 = search2->next;
			}
		}
		search = search->next;
	}
		
	return width;
}